package com.wbsxch.algorithm01;

import org.junit.Test;

import java.util.Arrays;

public class Test0101 {

    @Test
    public void test14(){
        System.out.println(lg(16));
    }

    public static int lg(int N){
        int x = 0;
        int total = 1;
        while ((total*=2)<=N){
            x++;
        }
        return x;
    }

    @Test
    public void test15() {
        int[] histogram = histogram(new int[]{1, 2, 3, 4, 5, 5}, 6);
        for (int i = 0; i < histogram.length; i++) {
            System.out.println(histogram[i]);
        }
    }

    public static int[] histogram(int a[],int m){
        int[] arr = new int[m];
        //Arrays.sort(a);
        /*if(a[a.length-1]<=m-1){

        }*/
        for (int i=0;i<m;i++){
            int num = 0;
            for(int j=0;j<a.length;j++){
                if(i==a[j]){
                    num++;
                }
            }
            arr[i] = num;
        }
        return arr;
    }

    @Test
    public void test16() {
        String s = exR1(6);
        System.out.println(s);
    }

    private String exR1(int n){
        if(n <= 0) return "";
        return exR1(n-3)+n+exR1(n-2)+n;
    }

    @Test
    public void test18() {
        System.out.println(mystery(2, 25));
        System.out.println(mystery(3, 11));
        System.out.println(mystery2(2, 25));
        System.out.println(mystery2(3, 11));
    }

    private static int mystery(int a,int b){
        if(b == 0) return 0;
        if(b%2 == 0) return mystery(a+a, b/2);
        return mystery(a+a, b/2)+a;
    }

    private static int mystery2(int a,int b){
        if(b == 0) return 1;
        if(b%2 == 0) return mystery(a*a, b/2);
        return mystery(a*a, b/2)*a;
    }

    @Test
    public void test19_1() {
        long[] arr = new long[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = f(i);
        }
        for (int i = 0; i < arr.length; i++) {
            long l = arr[i];
            System.out.println("i = "+l);
        }
    }
    @Test
    public void test19_2() {
        long[] arr = new long[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = f2(i);
        }
        for (int i = 0; i < arr.length; i++) {
            long l = arr[i];
            System.out.println(i+ " = "+l);
        }
    }

    private long f(int n){
        if(n == 0) return 0;
        if(n == 1) return 1;
        return f(n-1)+f(n-2);
    }

    private long f2(int n){
        long i1 = 0;
        long i2 = 1;
        if(n == i1) return i1;
        if(n == i2) return i2;
        long i3 = 0;
        for (int i = 1; i < n; i++) {
            i3 = i1 + i2;
            i1 = i2;
            i2 = i3;
        }
        return i3;
    }

    @Test
    public void test20() {

    }
}
